<?php

header( 'Content-Type:text/html;charset=utf-8 ');
require './Admin/Model/UserModel.php';
require 'Controller.php';
class UserController extends Controller{

    function listusers(){
       /* $data=array(array('uname'=>'zhangsan','sex'=>'male'),array('uname'=>'lisi','sex'=>'female'));*/
        $model=new UserModel();
        $data=$model->getUserList();
        $this->assign('users',$data);
        $this->display();
        //return $data;
    }
    function deluserpic($uid){
        $model = new UserModel();
        $data['users']=$model->getUser($uid);
        $filename=$data['users']['pic'];
        $filepath="./photos/".$filename;
        if(file_exists($filepath)) {
            unlink($filepath);
        }
    }
    function deluser()
    {
        $uid = $_GET['uid'];
        $this->deluserpic($uid);
        $model = new UserModel();
        $result = $model->delUser($uid);
        if ($result) {
            echo "删除成功！3秒后返回";
            header("refresh:3;url='admin.php?c=user&m=listusers");
        } else {
            echo "删除失败！3秒后返回";
            header("refresh:3;url='admin.php?c=user&m=listusers");
        }
    }
    function reguser(){

        $this->display();
    }
    function adduser(){
        $username=trim($_POST['uname']);
        $password=md5($_POST['upass']);
        $intro=$_POST['intro'];
        $pic=$_FILES['img'];
        $model=new UserModel();
        $result=$model->regUser($username,$password,$intro,$pic);
        if($result){
            echo "插入成功！3秒后返回";
            header("refresh:3;url='admin.php?c=user&m=listusers");
        }
        else{
            echo "插入失败！3秒后返回";
            header("refresh:3;url='admin.php?c=user&m=listusers");
        }
    }
    function edituser(){
        $uid=$_GET['uid'];
        $model=new UserModel();
        $data=$model->getUser($uid);
        $this->assign('users',$data);
        $this->display();
    }
    function upduser(){
        $uid=$_POST['uid'];
        $this->deluserpic($uid);
        $username=trim($_POST['uname']);
        $password=$_POST['upass'];
        $intro=$_POST['intro'];
        $pic=$_FILES['img'];
        $model=new UserModel();
        $result=$model->editUser($uid,$username,$password,$intro,$pic);
        if($result){
            echo "修改成功！3秒后返回";
            header("refresh:3;url='admin.php?c=user&m=listusers");
        }
        else{
            echo "修改失败！3秒后返回";
            header("refresh:3;url='admin.php?c=user&m=listusers");
        }

    }
}

?>